package com.yixin.hospital.config;

import com.yixin.hospital.common.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Value("${swagger.enabled}")
    private boolean enableSwagger;
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enableSwagger)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yixin.hospital.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getGlobalOperationParam());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 页面标题
                .title("hospital项目API文档")
                // 项目描述
                .description("hospital项目API文档")
                // 服务条款网址
                .termsOfServiceUrl("xxx")
                // 作者信息
                .contact(new Contact("cfh", "", ""))
                // 许可证
                .license("Apache 3.0")
                // 版本号
                .version("v1.0")
                .build();
    }

    private List<Parameter> getGlobalOperationParam(){
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameterList = new ArrayList<>();
        parameterBuilder
                .name(Constant.AUTHORIZATION)
                .description("token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        parameterList.add(parameterBuilder.build());
        return parameterList;
    }
}
